const { getReplyList, getReplyTotal, removeReply, addReply } = require('../../model/reply')


/**
 *  获取文章回复总数
 */
const replyTotal = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { posts_id } = req.query
    if (!posts_id) {
        output.code = 500
        output.msg = '参数错误:缺少参数posts_id'
        return res.send(output)
    }
    const result = await getReplyTotal({ posts_id })
    output.data = result
    res.send(output)
}
/**
 *  获取文章回复列表 
 */
const replyList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    // 文章id
    const { posts_id, page, size } = req.query
    if (!posts_id) {
        output.code = 500
        output.msg = "参数错误:缺少参数posts_id"
        return res.send(output)
    }
    const map = page && size ? { page, size } : {}
    map.posts_id = posts_id
    const result = await getReplyList(map)
    output.data = result
    res.send(output)
}

/**
 *  发布回复
 */
const replyAdd = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    // 获取登录账号id
    const { id: uid } = req.loginInfo
    const { posts_id, content } = req.body
    if (!posts_id || !content) {
        return res.send(output)
    }
    const replyData = { uid, posts_id, content, create_time: Date.now() }
    const result = await addReply(replyData)
    output.data = result
    res.send(output)
}


/**
 *  删除文章回复
 */
const replyRemove = async (req, res) => {
    const output = { code: 200, msg: '操作成功' }
    // 待删除数据id
    const { id } = req.query
    if (!id) {
        output.code = 500
        output.msg = "参数错误:缺少参数id"
        return res.send(output)
    }
    const result = await removeReply(id)
    output.data = result
    res.send(output)
}

module.exports = {
    replyList, replyRemove, replyAdd, replyTotal
}